package cn.edu.ctbu.datajpa1.service;

import cn.edu.ctbu.datajpa1.domain.Student;
import cn.edu.ctbu.datajpa1.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    public List<Student> findAll(){

        return studentRepository.findAll();
    }

    /**
     按id进行查询
     *@param id 主键，整数
     *@return
     */
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 按姓名查找
     * @param name
     * @return
     */
    public List<Student> findByName(String name){
        return studentRepository.findByNameLike(name);
    }

    /**
     * 按姓名和密码查找
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){
        return studentRepository.findByNameAndPassword(name,password);
    }

    public Student insert(Student student){

        studentRepository.save(student);
        return student;
    }

    public Student update(Student student){
        studentRepository.save(student);
        return student;
    }

    public void delete(Integer id){
        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }
    public void delete(Student student){
        studentRepository.delete(student);
    }


    public Student getById(Integer id) {
        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }
}


